package be.novelfaces.showcase.webdriver.util;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class WebDriverFactory {
	
	private static FirefoxDriver FIREFOX_DRIVER;
	private static InternetExplorerDriver INTERNET_EXPLORER_DRIVER;
	private static ChromeDriver CHROME_DRIVER;
	
	static {
		System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox");
		FIREFOX_DRIVER = new FirefoxDriver();
	}
	
	public static WebDriver getInstance(Browser browser){
		if(Browser.INTERNET_EXPLORER.equals(browser)){
			return getInternetExplorerDriver();
		} else if(Browser.CHROME.equals(browser)){
			return getChromeDriver();
		}
		return FIREFOX_DRIVER;
	}

	private static ChromeDriver getChromeDriver() {
		if(CHROME_DRIVER == null) {
			CHROME_DRIVER = new ChromeDriver();
		}
		return CHROME_DRIVER;
	}

	private static InternetExplorerDriver getInternetExplorerDriver() {
		if(INTERNET_EXPLORER_DRIVER == null){
			INTERNET_EXPLORER_DRIVER = new InternetExplorerDriver();
		}
		return INTERNET_EXPLORER_DRIVER;
	}
	
	public static void closeDrivers(){
		if (FIREFOX_DRIVER != null) {
			FIREFOX_DRIVER.close();
		}
		if (INTERNET_EXPLORER_DRIVER != null) {
			INTERNET_EXPLORER_DRIVER.close();
		}
		if (CHROME_DRIVER != null) {
			CHROME_DRIVER.close();
		}
	}

}
